﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GUESTSMANAGEMENT.Controller;
using Coolite.Ext.Web;

namespace GUESTSMANAGEMENT.View.Guets
{
    public partial class frmGuets : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore
        {
            get;
            set;
        }


        protected void FormSave(object sender, AjaxEventArgs e)
        {
            try
            {
                string id = this.txt_guestsId.Text;

                if (id.Equals(""))
                {
                    using (guestsmanagementEntities context = new guestsmanagementEntities())
                    {
                        gmguests guests = new gmguests();
                        guests.NMGUESTS = this.guestsName.Text.ToString();
                        guests.NMMAIL = this.guestsEmail.Text.ToString();
                        guests.NMPHONE = this.guestsPhone.Text.ToString();
                        Controller.Guests.addGuests(guests);
                        ClearWindow(Controls);
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                        FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Cadastro efetuado com sucesso!", IconCls = "", Clear2 = true });
                    }
                }
                else
                {
                    using (guestsmanagementEntities context = new guestsmanagementEntities())
                    {
                        gmguests guests = Controller.Guests.getByGuests(int.Parse(id));
                        guests.NMGUESTS = this.guestsName.Text.ToString();
                        guests.NMMAIL = this.guestsEmail.Text.ToString();
                        guests.NMPHONE = this.guestsPhone.Text.ToString();
                        Controller.Guests.updateGuests(guests);
                        this.winGuests.Hide();
                        ClearWindow(Controls);
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                        Ext.Msg.Alert("Alerta!", "Convidado <br>" + guests.NMGUESTS + " </br> atualizado com sucesso!").Show();
                        FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Convidado atualizado com sucesso!", IconCls = "", Clear2 = true });

                    }
                }
                }
                catch
                {
                    Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
                }
            }

        public void ShowGuests()
        {
            this.winGuests.Show();
        }

        public void SetGuests(gmguests guests)
        {
            this.txt_guestsId.Text = guests.CDGUESTS.ToString();
            this.guestsName.Text = guests.NMGUESTS;
            this.guestsEmail.Text = guests.NMMAIL;
            this.guestsPhone.Text = guests.NMPHONE;
        }

        protected void Close(object sender, AjaxEventArgs e)
        {
            this.winGuests.Hide();
            ClearWindow(Controls);
        }

        public void ClearWindow(ControlCollection AllControls)
        {
            foreach (Control c in AllControls)
            {
                if (c is TextField)
                {
                    ((TextField)c).Text = "";
                }
                if (c is ComboBox)
                {
                    ((ComboBox)c).Value = null;
                }
                if (c is Checkbox)
                {
                    ((Checkbox)c).Checked = true;
                }
                if (c is Hidden)
                {
                    ((Hidden)c).Text = "";
                }
                ClearWindow(c.Controls);
            }
        }
    }
}